package main

import (
	"flag"
	"log"
	"net/rpc"
	"strings"

	parser "aqielife.cn/spider/singleSpider/zhenai/parser"
	"aqielife.cn/spider/singleSpider/zhenai/scheduler"
	"aqielife.cn/spider/singleSpider/zhenaiDistributed/engine"
	itemSaverClient "aqielife.cn/spider/singleSpider/zhenaiDistributed/persist/client"
	"aqielife.cn/spider/singleSpider/zhenaiDistributed/rpcsupport"
	workerClient "aqielife.cn/spider/singleSpider/zhenaiDistributed/worker/client"
)

var (
	itemSaverHost = flag.String("itemsaver_host", "",
		"itemsaver host")

	workerHosts = flag.String("worker_hosts", "",
		"worker hosts (comma separated)")
)

func main() {
	flag.Parse()

	itemChan, err := itemSaverClient.ItemSaver(*itemSaverHost)
	if err != nil {
		panic(err)
	}
	hosts := strings.Split(*workerHosts, ",")
	pool := createClientPool(hosts)
	processor, err := workerClient.CreateProcessor(pool)
	if err != nil {
		panic(err)
	}
	log.Println("客户端已连接成功....")

	e := engine.ConcurrentEngine{
		Scheduler:        &scheduler.QueuedScheduler{},
		WorkerCount:      100,
		ItemChan:         itemChan,
		RequestProcessor: processor,
	}
	//e.Run(engine.Request{
	//	Url: 		"https://www.zhenai.com/zhenghun",
	//	ParserFunc: parser.ParseCityList,
	//})
	//测试单个城市
	//e.Run(engine.Request{
	//	Url: "https://www.zhenai.com/zhenghun/shanghai",
	//	ParserFunc: parser.ParseCity,
	//})

	e.Run(engine.Request{
		Url:    "https://album.zhenai.com/u/1365187484",
		Parser: parser.NewParseProfile("琪琪"),
	})
}

func createClientPool(hosts []string) chan *rpc.Client {
	var clients []*rpc.Client

	for _, host := range hosts {
		client, err := rpcsupport.NewClient(host)
		if err != nil {
			log.Printf("error connecting to %s: %v", host, err)
			continue
		}
		clients = append(clients, client)
	}

	out := make(chan *rpc.Client)

	go func() {
		for {
			for _, client := range clients {
				out <- client
			}
		}

	}()

	return out
}
